﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using BUS;
using System.IO;

namespace Presentation
{
    public partial class UserControlListKeys : UserControl
    {
        public UserControlListKeys()
        {
            InitializeComponent();
            _SetKey = new UserControlSetKeys();
            this.Controls.Add(_SetKey);
            _SetKey.Close+=new EventHandler(ShowThis);
            _InputFind = new CInputKeyShortcut();
            _InputFind.InstallFind(ref txtFindKey);
        }

        CConnect _connect = new CConnect();
        BUS.CInputKeyShortcut _InputFind;
        UserControlSetKeys _SetKey;

        private void cmdAddKey_Click(object sender, EventArgs e)
        {
            plAllControl.Hide();
            _SetKey.ThemKey();
        }

        private void ShowThis(object sender, EventArgs e)
        {
            HienThiDSKey();
            plAllControl.Show();
        }
        public void HienThiDSKey()
        {
            FileInfo _FileInfo = new FileInfo(BUS.COption.DataPath);
            if (_FileInfo.Exists)
            {
                this.Enabled = true;
                dgvListKeys.DataSource = CConnect.LayDanhSachKey(txtFindKey.Text);
                if (dgvListKeys.RowCount == 0)
                {
                    cmdDeleteKey.Enabled = cmdEditKey.Enabled = cmdExport.Enabled = false;
                }
                else
                {
                    cmdDeleteKey.Enabled = cmdEditKey.Enabled = cmdExport.Enabled = true;
                }
            }
            else
            {
                this.Enabled = false;
                MessageBox.Show("Couldn't found file data! Please choose another path in option!", "Warning", MessageBoxButtons.OK);
            }
        }

        private void cmdEditKey_Click(object sender, EventArgs e)
        {
            plAllControl.Hide();
            _SetKey.SuaKey(new CKeys(dgvListKeys.SelectedRows[0].Cells["Shortcutkey"].Value.ToString(), dgvListKeys.SelectedRows[0].Cells["Runningkey"].Value.ToString(), Convert.ToInt32(dgvListKeys.SelectedRows[0].Cells["Delay"].Value), Convert.ToInt32(dgvListKeys.SelectedRows[0].Cells["Loop"].Value), dgvListKeys.SelectedRows[0].Cells["Description"].Value.ToString()));
            HienThiDSKey();
        }

        private void cmdDeleteKey_Click(object sender, EventArgs e)
        {
            BUS.CConnect.XoaKeys(dgvListKeys);
            HienThiDSKey();
        }

        private void cmdInport_Click(object sender, EventArgs e)
        {
            BUS.CConnect.Inport();
            HienThiDSKey();
        }

        private void cmdExport_Click(object sender, EventArgs e)
        {
            BUS.CConnect.Export(dgvListKeys);
        }

        private void cmdCleadFind_Click(object sender, EventArgs e)
        {
            txtFindKey.Text = null;
        }

        private void txtFindKey_TextChanged(object sender, EventArgs e)
        {
            HienThiDSKey();
        }
    }
}
